package com.huashi.dealer.modular.controller.api.article;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.Article;
import com.huashi.dealer.commonEntity.ArticleCategory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.article.service.ArticleService;
import com.huashi.dealer.modular.business.article.param.ArticleResponseParam;
import com.huashi.dealer.modular.business.article.vo.ArticleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API文章控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API文章控制器")
@RestController("apiArticleController")
@RequestMapping("/api/article")
@Validated
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 文章列表
     */
    @GetMapping("/lists")
    @Operation(summary = "文章列表")
    public CommonResult<Page<Article>> getArticleList(@RequestParam(required = false) Long categoryId) {
        try {
            Page<Article> responseParam = articleService.getArticleList(categoryId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取文章列表失败：" + e.getMessage());
        }
    }

    /**
     * 分类列表
     */
    @GetMapping("/index")
    @Operation(summary = "分类列表")
    public CommonResult<List<ArticleCategory>> getCategoryList() {
        try {
            List<ArticleCategory> responseParam = articleService.getCategoryList();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取分类列表失败：" + e.getMessage());
        }
    }

    /**
     * 文章列表（无参数）
     */
    @GetMapping("/articleList")
    @Operation(summary = "文章列表（无参数）")
    public CommonResult<ArticleResponseParam> getArticleListWithoutParams() {
        try {
            ArticleResponseParam responseParam = articleService.getArticleListWithoutParams();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取文章列表失败：" + e.getMessage());
        }
    }

    /**
     * 文章详情
     */
    @GetMapping("/detail/{articleId}")
    @Operation(summary = "文章详情")
    public CommonResult<ArticleVo> getArticleDetail(@PathVariable Long articleId) {
        try {
            ArticleVo responseParam = articleService.getArticleDetail(articleId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取文章详情失败：" + e.getMessage());
        }
    }

    /**
     * 热门文章
     */
    @GetMapping("/hot")
    @Operation(summary = "热门文章")
    public CommonResult<ArticleResponseParam> getHotArticles(@RequestParam(required = false) String token,
                                                             @RequestParam(required = false) Integer limit) {
        try {
            ArticleResponseParam responseParam = articleService.getHotArticles(token, limit);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取热门文章失败：" + e.getMessage());
        }
    }

    /**
     * 推荐文章
     */
    @GetMapping("/recommend")
    @Operation(summary = "推荐文章")
    public CommonResult<ArticleResponseParam> getRecommendArticles(@RequestParam(required = false) String token,
                                                                  @RequestParam(required = false) Integer limit) {
        try {
            ArticleResponseParam responseParam = articleService.getRecommendArticles(token, limit);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取推荐文章失败：" + e.getMessage());
        }
    }

    /**
     * 搜索文章
     */
    @GetMapping("/search")
    @Operation(summary = "搜索文章")
    public CommonResult<ArticleResponseParam> searchArticles(@RequestParam String keyword,
                                                            @RequestParam(required = false) String token,
                                                            @RequestParam(required = false) Long categoryId,
                                                            @RequestParam(required = false) Integer page,
                                                            @RequestParam(required = false) Integer listRows) {
        try {
            ArticleResponseParam responseParam = articleService.searchArticles(keyword, token, categoryId, page, listRows);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("搜索文章失败：" + e.getMessage());
        }
    }
} 